home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / partition.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  2.2 KB  |  108 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  partition.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_PARTITION_H
  16. #define LEDA_PARTITION_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // partition   (union find)
  20. //------------------------------------------------------------------------------
  21.  
  22. #include <LEDA/basic.h>
  23.  
  24.  
  25. class partition_node {
  26.  
  27. friend class partition;
  28.  
  29. partition_node* father;
  30. partition_node* next;
  31. int size;
  32. GenPtr info;
  33.  
  34. public:
  35.  
  36. partition_node(GenPtr x, partition_node* n)  
  37.   father=0; size=1; info=x; next = n; 
  38.  }
  39.  
  40.   LEDA_MEMORY(partition_node)
  41.  
  42. };
  43.  
  44.  
  45. // a partition item is a pointer to a partition node:
  46.  
  47. typedef partition_node* partition_item;
  48.  
  49.  
  50.  
  51. class partition {
  52.  
  53. virtual void clear_inf(GenPtr&) const {}
  54.  
  55. partition_item used_items;                 // List of used partition items
  56.  
  57. public:  // operations 
  58.  
  59. void  union_blocks(partition_item,partition_item);
  60. partition_item find(partition_item);
  61.  
  62. partition_item make_block(GenPtr x = nil) 
  63. { used_items = new partition_node(x,used_items); 
  64.   return used_items; 
  65.  }
  66.  
  67. int  same_block(partition_item a, partition_item b) 
  68. { return find(a)==find(b); }
  69.  
  70. GenPtr   inf(partition_item a) { return find(a)->info; }
  71.  
  72. void  set_inf(partition_item a, GenPtr x) { find(a)->info = x; }
  73.  
  74. void clear();                      // deletes all used items
  75.  
  76. partition() { used_items = 0; }  
  77. virtual ~partition() { clear(); }
  78.  
  79. };
  80.  
  81.  
  82. //------------------------------------------------------------------------------
  83. // PARTITION  (named partitions)
  84. //-----------------------------------------------------------------------------
  85.  
  86. template <class type>
  87.  
  88. class PARTITION : public partition {
  89.  
  90. void clear_inf(GenPtr& x) const { Clear(ACCESS(type,x)); }
  91.  
  92. public:
  93.  
  94. partition_item make_block(type x) { return partition::make_block(Convert(x)); }
  95.  
  96. type  inf(partition_item a)       { return (type)partition::inf(a); }
  97.  
  98. void  set_inf(partition_item a, type x) { partition::set_inf(a,Convert(x)); }
  99.  
  100.  PARTITION() {}
  101. ~PARTITION() {}
  102. };
  103.  
  104.  
  105. #endif
  106.